<?php


require_once(DIR_LIB."VsCore".DIR_SEP."vsauth.php");

require_once(DIR_LIB."VsCore".DIR_SEP."vssession.php");
require_once(DIR_LIB."VsCore".DIR_SEP."db.php");

class VsAuthData {

	private $_sqlTable = NULL;

	private $_userId = 0;
	private $_userName = 'anonymous';
	private $_userPassword = '';	// Encripted
	private $_userLevel = 0;
	private $_userMail = '';
	private $_userLastLogin = 0;
	private $_userLastLoginHost = '';

	static function Anonymous() { return new VsAuthData(); }
	
	static function makeUserById($id, $sqlTable='users') {
		$authData = new VsAuthData($sqlTable);
		$db = VsDatabase::Instance();

		$q = "SELECT * FROM `".$sqlTable."` WHERE id='".$id."'";
		//echo $q;
		$res = $db->query($q);

		if(mysql_num_rows($res)>0) {
			$rUserData = mysql_fetch_object($res);

			$authData->_userId = $rUserData->id;
			$authData->_userName = $rUserData->username;
			$authData->_userPassword = $rUserData->password;
			$authData->_userLevel = $rUserData->level;
			$authData->_userMail = $rUserData->email;
			$authData->_userLastLogin = $rUserData->last_visit_time;
			$authData->_userLastLoginHost = $rUserData->last_visit_host;
		}

		return $authData;
	}
	//==========================================================================

	static function makeUserByName($userName, $sqlTable='users') {
		$authData = new VsAuthData($sqlTable);
		$db = VsDatabase::Instance();

		$q = "SELECT * FROM `".$sqlTable."` WHERE username='".$userName."'";
		//echo $q;
		$res = $db->query($q);

		if(mysql_num_rows($res)>0) {
			$rUserData = mysql_fetch_object($res);

			$authData->_userId = $rUserData->id;
			$authData->_userName = $rUserData->username;
			$authData->_userPassword = $rUserData->password;
			$authData->_userLevel = $rUserData->level;
			$authData->_userMail = $rUserData->email;
			$authData->_userLastLogin = $rUserData->last_visit_time;
			$authData->_userLastLoginHost = $rUserData->last_visit_host;
		}

		return $authData;
	}
	//==========================================================================

	static function makeUserByMail($mail, $sqlTable='users') {
		$authData = new VsAuthData($sqlTable);
		$db = VsDatabase::Instance();

		$q = "SELECT * FROM `".$sqlTable."` WHERE email='".$mail."'";
		//echo $q;
		$res = $db->query($q);

		if(mysql_num_rows($res)>0) {
			$rUserData = mysql_fetch_object($res);

			$authData->_userId = $rUserData->id;
			$authData->_userName = $rUserData->username;
			$authData->_userPassword = $rUserData->password;
			$authData->_userLevel = $rUserData->level;
			$authData->_userMail = $rUserData->email;
			$authData->_userLastLogin = $rUserData->last_visit_time;
			$authData->_userLastLoginHost = $rUserData->last_visit_host;
		}

		return $authData;
	}
	//==========================================================================

	static function makeUserFromForm($form, $sqlTable='users') {
		$authData = new VsAuthData($sqlTable);

		$authData->_userId = $form['login_id'];
		$authData->_userName =  $form['uname'];
		$authData->_userPassword =  md5($form['passwd1']);
		$authData->_userLevel =  $form['level'];
		$authData->_userMail =  $form['email'];

		//$authData->_userLastLogin = $rUserData->last_visit_time;
		//$authData->_userLastLoginHost = $rUserData->last_visit_host;
		
		return $authData;
	}
	//==========================================================================

	function isValid() { return $this->_userLevel ? TRUE : FALSE; }
	function confirmPassword($password) { return ($this->_userPassword == md5($password)); }

	function id() { return $this->_userId; }
	function userId() { return $this->id(); }
	function user() { return $this->_userName; }
	function userName() { return $this->user(); }
	function name() { return $this->user(); }
	function password() { return $this->_userPassword; }
	function level() { return $this->_userLevel; }
	function userLevel() { return $this->level(); }
	function email() { return $this->_userMail; }
	function lastLogin() { return $this->_userLastLogin; }
	function lastLoginHost() { return $this->_userLastLoginHost; }

	// Class Constructor
	private function __construct($sqlTable = 'users') {
		$this->_sqlTable = $sqlTable;
	}
	//=========================================================================

	// For Use In VsAuth Class
	function setAuthSession($vsAuth) {
		if($vsAuth->userName() !== $this->_userName)
			return;

		$db = VsDatabase::Instance();
		$_vsSession = VsSession::Instance();
		
		$q = "UPDATE `".$this->_sqlTable."` SET `session_id`='".$_vsSession->id()."' WHERE `username`='".$this->_userName."'";
		//echo $q;
		$res = $db->query($q);
	}
	//======================================================================================

	function confirmAuthSession($vsAuth) {
		if($vsAuth->userName() !== $this->_userName)
			return FALSE;

		$db = VsDatabase::Instance();
		$_vsSession = VsSession::Instance();

		$q = "SELECT `session_id` FROM `".$this->_sqlTable."` WHERE username='".$this->_userName."'";
		$res = $db->query($q);

		//echo $q;
		if(@mysql_num_rows($res)<1)
			return FALSE;
		else {
			$row = mysql_fetch_object($res);
		
			return ($row->session_id==$_vsSession->id());
		}
	}
	//=====================================================================================

	function setLastLogin() {
		$q = "UPDATE `".$this->_sqlTable."` SET `last_visit_time`=NOW(), `last_visit_host`='".$_SERVER['REMOTE_ADDR']."' WHERE id=".$this->_userId;

		$db = VsDatabase::Instance();
		$db->query($q);
	}
	//=======================================================================================================================

	function setPassword($newPass) {
		$auth = VsAuth::Instance();
		if(($auth->level()<=5))			//  && $auth->id()!=$this->_authId
			return FALSE;

		$this->_userPassword = md5($newPass);
		$this->updatePassword();

		return TRUE;
	}
	//======================================================================================================================

	function save() {
		if($this->_userId > 0)
			$this->update();
		else
			$this->insert();

		return $this->_id;
	}
	//=======================================================================================

	private function insert() {
		$auth = VsAuth::Instance();
		if(($auth->level()<=5) && $auth->id()!=$this->_authId)
			return;

		$q = "INSERT INTO `".$this->_sqlTable."` (`username`, `password`, `level`, `email`) VALUES('".$this->_userName."', '".$this->_userPassword."', '".$this->_userLevel."', '".$this->_userMail."')";
		//echo $q;
		//exit;

		$db = VsDatabase::Instance();
		$db->query($q);
		//echo mysql_error();

		$this->_id = mysql_insert_id();
	}
	//=======================================================================================================================

	private function update() {
		$auth = VsAuth::Instance();
		if(($auth->level()<=5) && $auth->id()!=$this->_authId)
			return;

		$q = "UPDATE `".$this->_sqlTable."` SET `products_categories_id`='".$this->_categoryId."', `name`='".addslashes($this->_name)."', `info`='".addslashes($this->_info)."', date=NOW() WHERE id=".$this->_id;

		$db = VsDatabase::Instance();
		$db->query($q);

	}
	//=======================================================================================================================

	private function updatePassword() {
		$q = "UPDATE `".$this->_sqlTable."` SET `password`='".$this->_userPassword."' WHERE id=".$this->_userId;

		$db = VsDatabase::Instance();
		$db->query($q);
	}
	//======================================================================================================================

};

?>
